package com.canbot.u05.activity.firstguide

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.tool.RecyclerItemDecoration
import com.canbot.u05.activity.loge
import com.canbot.u05.activity.showToast
import com.canbot.u05.adapter.SceneChooseAdapter
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.bean.Scene
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.OkHttpUtils
import com.canbot.u05.utils.PathConst
import com.canbot.u05.utils.ToastTools
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_scene_choose.*
import java.util.*

/**
 * desc: 场景选择界面
 * author: xxl
 * date: 2019/6/19
 */
class SceneChooseActivity : BaseActivityNoTitle(), SceneChooseAdapter.OnItemClickListener {
    private var sceneName: String = ""
    private var loadingDialog: Dialog? = null
    private lateinit var sceneChooseAdapter: SceneChooseAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scene_choose)

        getData()

        val adminPassword = intent.getStringExtra("password")

        val layoutManager = GridLayoutManager(this, 5)
        rv_scene.layoutManager = layoutManager
        rv_scene.addItemDecoration(RecyclerItemDecoration(5, resources.getDimension(R.dimen.px22).toInt(), resources.getDimension(R.dimen.px8).toInt(), false))
        sceneChooseAdapter = SceneChooseAdapter(this, null)
        rv_scene.adapter = sceneChooseAdapter

        sceneChooseAdapter.setItemClickListener(this)

        btn_next_step.setOnClickListener {

            if (!sceneName.isEmpty()) {
                val intent = Intent(this@SceneChooseActivity, SceneModuleActivity::class.java)
                intent.putExtra("password", adminPassword)
                intent.putExtra("scene", sceneName)
                startActivity(intent)
            } else {
                showToast(getString(R.string.please_choose_scene))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.ADMIN_AUDIO_PATH + "gzcjxz_szscyd_u05_001.mp3")
    }

    /**
     * 获取场景数据
     */
    private fun getData() {
        loadingDialog = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.request))
        val params = HashMap<String, String>()
        params["get_data"] = "get_data"
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "scene", params, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                loge("result:$result")
                val jsonResponse = JSON.parseObject(result, JsonResponse::class.java)
                if (jsonResponse.rc == -1) {
                    loge("获取场景信息失败")
                    cancelDialog()
                } else if (jsonResponse.rc == 0) {
                    loge("获取场景信息成功")
                    val sceneString = jsonResponse.result.toString()
                    val scenes = JSON.parseArray(sceneString, Scene::class.java)
                    val scene = Scene()
                    scene.sceneName = getString(R.string.scene_name_zdy)
                    scenes.add(scene)
                    runOnUiThread { sceneChooseAdapter.setSceneList(scenes) }
                    cancelDialog()
                }
            }

            override fun onFailure() {
                loge("queryAdmin onFailure")
                ToastTools.newInstance(this@SceneChooseActivity).showText(getString(R.string.service_error_hint))
                cancelDialog()
            }
        })
    }

    override fun onItemClick(sceneName: String) {

        this.sceneName = sceneName
    }

    fun back(view: View) = finish()

    override fun onPause() {
        super.onPause()
        cancelDialog()
    }

    /**
     * 取消加载的Dialog
     */
    private fun cancelDialog() {
        runOnUiThread {
            if (loadingDialog != null && loadingDialog!!.isShowing) {
                loadingDialog?.cancel()
            }
        }

    }
}
